package com.md.album.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.md.album.R;
import com.md.network.api.Album;

import java.util.ArrayList;

public class AlbumListAdapter extends RecyclerView.Adapter<AlbumListAdapter.AlbumListViewHolder> {

    private Context mContext;
    private ArrayList<Album> datas;
    private OnItemClickListener listener;

    public AlbumListAdapter(Context mContext, OnItemClickListener listener,ArrayList<Album> datas) {
        this.mContext = mContext;
        this.datas = datas;
        this.listener = listener;
    }

    @NonNull
    @Override
    public AlbumListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_album_list, parent, false);
        return new AlbumListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AlbumListViewHolder holder, final int position) {
//        if (position == 0) {
//            holder.ivAlbum.setImageDrawable(mContext.getResources().getDrawable(R.drawable.album_add_audio));
//        } else {
            final Album curAlbum = datas.get(position);
            if (curAlbum != null) {
                holder.tvAlbumName.setText(curAlbum.getName());
                Glide.with(mContext)
                        .load(curAlbum.getCoverUrl())
                        .placeholder(R.drawable.selfalbum_cover_default)
                        .into(holder.ivAlbum);
            }
//        }
        holder.cltItemRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener !=null){
                    listener.onItemClick(curAlbum,position);
                }
//                if (position == 0){
//
//                }else {
//                    boolean isSuc = SelfAlbumManager.getIns().saveAudioToAlbum(String.valueOf(curAlbum.getId()),wrapperAudio.audio);
//                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class AlbumListViewHolder extends RecyclerView.ViewHolder {
        public ConstraintLayout cltItemRoot;
        public ImageView ivAlbum;
        public TextView tvAlbumName;
        public AlbumListViewHolder(@NonNull View itemView) {
            super(itemView);
            cltItemRoot = itemView.findViewById(R.id.cltItemRoot);
            ivAlbum = itemView.findViewById(R.id.ivAlbum);
            tvAlbumName = itemView.findViewById(R.id.tvAlbumName);
        }
    }

    public interface OnItemClickListener{
        void onItemClick(Album curAlbum, int curPos);
    }
}
